package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Feedback;
import com.example.exception.BusinessException;
import com.example.mapper.FeedbackMapper;
import com.example.service.FeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    @Transactional
    public boolean submitFeedback(Feedback feedback) {
        if (feedback.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        feedback.setStatus(0);  // 设置为待处理
        return save(feedback);
    }

    @Override
    @Transactional
    public boolean replyFeedback(Long id, String reply, Long replyUserId) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            throw new BusinessException("反馈不存在");
        }
        
        feedback.setReply(reply);
        feedback.setReplyUserId(replyUserId);
        feedback.setReplyTime(LocalDateTime.now());
        feedback.setStatus(2);  // 设置为已处理
        
        return updateById(feedback);
    }

    @Override
    @Transactional
    public boolean updateStatus(Long id, Integer status) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            throw new BusinessException("反馈不存在");
        }
        
        feedback.setStatus(status);
        return updateById(feedback);
    }

    @Override
    @Transactional
    public boolean deleteFeedback(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional
    public boolean batchDeleteFeedbacks(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("反馈ID列表不能为空");
        }
        return removeByIds(Arrays.asList(ids));
    }

    @Override
    public Page<Feedback> getFeedbackList(Integer pageNum, Integer pageSize, Long userId, Integer type, Integer status) {
        Page<Feedback> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        
        if (userId != null) {
            wrapper.eq(Feedback::getUserId, userId);
        }
        if (type != null) {
            wrapper.eq(Feedback::getType, type);
        }
        if (status != null) {
            wrapper.eq(Feedback::getStatus, status);
        }
        
        wrapper.orderByDesc(Feedback::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public Feedback getFeedbackInfo(Long id) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            throw new BusinessException("反馈不存在");
        }
        return feedback;
    }
} 